package com.ruitu.health_2.mvp.presenter;

import com.ruitu.arad.base.BasePresenter;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_2.mvp.model.ProductModel;
import com.ruitu.health_2.ui.activity.CouponListActivity;
import com.ruitu.health_2.ui.activity.ProductDetailActivity;
import com.ruitu.health_2.ui.common.ProductListActivity;
import com.ruitu.router_module.bean.BaseResult2;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.order.ConfirmOrder;
import com.ruitu.router_module.bean.product.Coupon;
import com.ruitu.router_module.bean.product.ProDetail;
import com.ruitu.router_module.bean.product.ProHome;
import com.ruitu.router_module.bean.product.Sku;
import com.ruitu.router_module.bean.system.Common;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

public class ProductPresenter {

    public static class ProGridP extends BasePresenter<ProductListActivity, ProductModel.ProGridM> {
        public Disposable reqProListByName(int size, int num, String name) {
            return m.reqProListByName(size, num, name).subscribe(new Consumer<BaseResult2<PageModel2<ProHome>>>() {
                @Override
                public void accept(BaseResult2<PageModel2<ProHome>> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(1, true, result.getData());
                    } else {
                        ToastUtils.showShortSafe(result.getMessage());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    throwable.printStackTrace();
                }
            });
        }

        public Disposable reqProListByColumn(int size, int num, String columnId) {
            return m.reqProListByColumn(size, num, columnId).subscribe(new Consumer<BaseResult2<PageModel2<ProHome>>>() {
                @Override
                public void accept(BaseResult2<PageModel2<ProHome>> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(11, true, result.getData());
                    } else {
                        ToastUtils.showShortSafe(result.getMessage());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    throwable.printStackTrace();
                }
            });
        }
    }

    public static class ProDetailP extends BasePresenter<ProductDetailActivity, ProductModel.ProDetailM> {

        public Disposable reqProDetail(String id) {
            return m.reqProDetail(id).subscribe(new Consumer<BaseResult2<ProDetail>>() {
                @Override
                public void accept(BaseResult2<ProDetail> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(1, true, result.getData());
                    } else {
                        ToastUtils.showShortSafe(result.getMessage());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    throwable.printStackTrace();
                }
            });
        }

        public Disposable reqSkuList(String pid) {
            return m.reqSkuList(pid).subscribe(new Consumer<BaseResult2<List<Sku>>>() {
                @Override
                public void accept(BaseResult2<List<Sku>> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(3, true, result.getData());
                    } else {
                        ToastUtils.showShortSafe(result.getMessage());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    throwable.printStackTrace();
                    v.hideProgress();
                }
            });
        }

        public Disposable reqAddToCart(RequestBody body) {
            return m.reqAddToCart(body).subscribe(new Consumer<BaseResult2<String>>() {
                @Override
                public void accept(BaseResult2<String> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(2, true, null);
                        ToastUtils.showLongSafe("已加入购物车");
                    } else {
                        v.onReqComplete(2, false, null);
                        ToastUtils.showShortSafe(result.getMessage());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    v.onReqComplete(2, false, null);
                    ToastUtils.showLongSafe("操作失败,请重试");
                    throwable.printStackTrace();
                }
            });
        }

        public Disposable reqBuyNow(String uId, String pId, String type, String skuId, String buyNum) {
            return m.reqBuyNow(uId, pId, type, skuId, buyNum).subscribe(new Consumer<BaseResult2<ConfirmOrder>>() {
                @Override
                public void accept(BaseResult2<ConfirmOrder> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(4, true, result.getData());
                    } else {
                        v.onReqComplete(4, false, null);
                        ToastUtils.showShortSafe(result.getMessage());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    throwable.printStackTrace();
                    v.hideProgress();
                    v.onReqComplete(4, false, null);
                }
            });
        }

        public Disposable reqAddCollect(RequestBody body) {
            return m.reqAddCollect(body).subscribe(new Consumer<BaseResult2<Common>>() {
                @Override
                public void accept(BaseResult2<Common> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(5, true, result.getData());
                        ToastUtils.showLongSafe("已加入收藏");
                    } else {
                        v.onReqComplete(5, false, null);
                        ToastUtils.showShortSafe(result.getMessage());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    v.onReqComplete(5, false, null);
                    ToastUtils.showLongSafe("操作失败,请重试");
                    throwable.printStackTrace();
                }
            });
        }

        public Disposable reqDelCollect(RequestBody body) {
            return m.reqDelCollect(body).subscribe(new Consumer<BaseResult2<String>>() {
                @Override
                public void accept(BaseResult2<String> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(6, true, null);
                        ToastUtils.showLongSafe("已取消收藏");
                    } else {
                        v.onReqComplete(6, false, null);
                        ToastUtils.showShortSafe(result.getMessage());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    v.onReqComplete(6, false, null);
                    ToastUtils.showLongSafe("操作失败,请重试");
                    throwable.printStackTrace();
                }
            });
        }
    }

    public static class CouponListP extends BasePresenter<CouponListActivity, ProductModel.CouponListM> {
        public Disposable reqCouponList(int size, int num) {
            return m.reqCouponList(size, num).subscribe(new Consumer<BaseResult2<PageModel2<Coupon>>>() {
                @Override
                public void accept(BaseResult2<PageModel2<Coupon>> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(1, true, result.getData());
                    } else {
                        ToastUtils.showShortSafe(result.getMessage());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    ToastUtils.showShortSafe("获取优惠券异常");
                    throwable.printStackTrace();
                }
            });
        }

        public Disposable reqGetCoupon(RequestBody body) {
            return m.reqGetCoupon(body).subscribe(new Consumer<BaseResult2<String>>() {
                @Override
                public void accept(BaseResult2<String> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(2, true, result.getData());
                    } else {
                        ToastUtils.showShortSafe(result.getMessage());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    throwable.printStackTrace();
                }
            });
        }
    }
}
